<?php
/**
 * 公共服务(提供一些公共方法,可以使用框架内部类)
 * panglishan
 * 2020.08.01
 */
namespace App\Services\Tools;

use App\Exceptions\BasicException;
use Carbon\Carbon;

class CommonService
{


    //获取时间转化数组--用于搜索
    static function transTime($start_at,$end_at)
    {
        $res = array();
        $res['start_at'] = Carbon::parse('1970-01-01 00:00:00')->timestamp;
        $res['end_at'] = Carbon::parse('9999-01-01 00:00:00')->timestamp;
        if(!empty($start_at)) $res['start_at'] = Carbon::parse($start_at)->timestamp;
        if(!empty($end_at)) $res['end_at'] = Carbon::parse($end_at)->timestamp;
        return $res;
    }

    //获得今天起止时间
    static function getTodayTime()
    {
        $res = array();
        $res['start_at'] = Carbon::now()->startOfDay()->timestamp;
        $res['end_at'] = Carbon::now()->endOfDay()->timestamp;
        return $res;
    }

    //获得昨天的起止时间
    static function getYesterdayTime()
    {
        $res = array();
        $res['start_at'] = Carbon::now()->startOfDay()->timestamp - 86400;
        $res['end_at'] = Carbon::now()->endOfDay()->timestamp - 86400;
        return $res;
    }

    //获取本周的起止时间
    static function getThisWeekTime()
    {
        $res = array();
        $res['start_at'] = Carbon::now()->startOfWeek()->timestamp;
        $res['end_at'] = Carbon::now()->endOfWeek()->timestamp;
        return $res;
    }

    //获取上周的起止时间
    static function getLastWeekTime()
    {
        $res = array();
        $res['start_at'] = Carbon::now()->startOfWeek()->timestamp - 604800;
        $res['end_at'] = Carbon::now()->endOfWeek()->timestamp - 604800;
        return $res;
    }

    //转换统计的时间,默认是今天的时间
    static function transStaticTime($start_at, $end_at)
    {
        $res = array();
        $default_time = self::getTodayTime();

        $res['start_at'] = $default_time['start_at'];
        $res['end_at'] = $default_time['end_at'];

        if (!empty($start_at)) $res['start_at'] = Carbon::parse($start_at)->timestamp;
        if (!empty($end_at)) $res['end_at'] = Carbon::parse($end_at)->timestamp;
        return $res;
    }

    //转换统计的时间,默认是今天的时间
    static function transStaticWeekTime($start_at, $end_at)
    {
        $res = array();
        $default_time = self::getThisWeekTime();

        $res['start_at'] = $default_time['start_at'];
        $res['end_at'] = $default_time['end_at'];

        if (!empty($start_at)) $res['start_at'] = Carbon::parse($start_at)->timestamp;
        if (!empty($end_at)) $res['end_at'] = Carbon::parse($end_at)->timestamp;
        return $res;
    }

    //秒数转成天或小时
    static function time_format($time) {
        $str = '';
        if ($time >= 86400) {
            $day = floor(bcdiv($time, 86400));
            $time = bcmod($time, 86400);
            $str .= $day . '天';
        }
        if ($time >= 3600) {
            $hour = floor(bcdiv($time, 3600));
            $time = bcmod($time, 3600);
            $str .= $hour . '小时';
        } else {
            $hour = 0;
        }
        if ($time >= 60) {
            $minute = floor(bcdiv($time, 60));
            $second = bcmod($time, 60);
            $str .= $minute . '分' . $second . '秒';
        } else {
            $second = $time;
            $str .= $second . "秒";
        }
        return $str;

    }

    //获取奖品的等级
    static function giftRank($money = 0) {
        if ($money < 500) {
            return '白色';
        } elseif ($money >= 500 && $money < 5000) {
            return '绿色';
        } elseif ($money >= 5000 && $money <= 29998) {
            return '蓝色';
        } elseif ($money > 29998 && $money < 200000) {
            return '紫色';
        } else {
            return '橙色';
        }
    }
    //获取支付方式
    static function payType($pay_type = 0) {
        switch ($pay_type){
            case '1':
                return '支付宝';
                break;
            case '2':
                return '微信';
                break;
            case '3':
                return '苹果内购';
                break;
            case '4':
                return 'H5支付宝';
                break;
            case '5':
                return 'H5微信';
                break;
            default:
                return '敬请期待';
        }
    }

    //下载渠道数组配置
    static  function  downloadChannel(){
            $channel =[
                '1'=>'huya',
            ];
            return $channel;
    }

    //中国省市自治区直辖市

    static  function  China(){
        $china =[
            [
                'count_user_ip'=>0,
                'address'=>'河北省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'山西省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'辽宁省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'吉林省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'黑龙江省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'江苏省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'浙江省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'安徽省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'福建省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'江西省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'山东省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'河南省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'湖北省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'湖南省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'广东省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'海南省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'四川省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'贵州省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'云南省',
            ],

            [
                'count_user_ip'=>0,
                'address'=>'陕西省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'甘肃省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'青海省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'台湾省',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'内蒙古自治区',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'广西壮族自治区',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'西藏自治区',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'宁夏回族自治区',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'新疆维吾尔自治区',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'香港特别行政区',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'澳门特别行政区',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'北京市',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'天津市',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'上海市',
            ],
            [
                'count_user_ip'=>0,
                'address'=>'重庆市',
            ],
        ];
    return $china;
    }

    static public function pwdHash($str_password = '', $real_password = '', $type = false) {
        if (!$str_password && !$real_password) return false;

        $hasher = new PasswordHashService(8, false);
        if (!$type) {
            return $hasher->HashPassword($str_password);
        } else {
            return $hasher->CheckPassword($str_password, $real_password);
        }
    }

    static public function getMillisecond() {
        list($s1, $s2) = explode(' ', microtime());
        return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
    }

}